package com.哈希表;

import java.util.HashSet;
import java.util.Set;

public class 快乐数 {
   static class Solution {


        /**
         * 关键点是当n重复时就进入的循环，
         * @param n
         * @return
         */
        public boolean isHappy(int n) {
            Set<Integer> set = new HashSet<>();
            while ( n != 1 && !set.contains(n)){
                set.add(n);
                n = this.sum(n);
            }
          return n==1;
        }
        public int sum(int n ){
            int and = 0;
            while (n != 0 ){
                //个位
                int unit = n %10;
                //去掉个位
                n = n/10;
                and += unit*unit;
            }
            return and;
        }

       public static void main(String[] args) {
           Solution solution = new Solution();
           System.out.println(solution.isHappy(19));
       }
    }


}
